package View;

import Model.UsuarioBean;
import View.acao.FazerRetirada;
import java.awt.HeadlessException;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

public class TelaPrincipal extends javax.swing.JFrame {

    private static UsuarioBean usuarioLogado = null;

    public static UsuarioBean getUsuarioLogado() {
        return usuarioLogado;
    }

    public TelaPrincipal() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    public TelaPrincipal(UsuarioBean usuarioLogado) {
        initComponents();
        this.setLocationRelativeTo(null);
        TelaPrincipal.usuarioLogado = usuarioLogado;
        admin();
    }

    //Metodo que checa se o usuario é o administrador para adicionar uns menus extras
    private void admin() {
        if (usuarioLogado != null && usuarioLogado.isAdmin()) {
            mn_cad_usuario.setEnabled(true);
        }
    }

    public static JDesktopPane getjDesktopPane1() {
        return jDesktopPane1;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mn_cad_cliente = new javax.swing.JMenuItem();
        mn_cad_titulo = new javax.swing.JMenuItem();
        mn_cad_genero = new javax.swing.JMenuItem();
        mn_cad_produtora = new javax.swing.JMenuItem();
        mn_cad_midia = new javax.swing.JMenuItem();
        mn_cad_usuario = new javax.swing.JMenuItem();
        mn_retirada = new javax.swing.JMenu();
        mn_todas_retirada = new javax.swing.JMenuItem();
        mn_cadastrar_Retirada = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Gerenciador de Locadora");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jMenu1.setText("Arquivo");

        jMenuItem1.setText("Logoff");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Sair");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Cadastros");

        mn_cad_cliente.setText("Cliente");
        mn_cad_cliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_clienteActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_cliente);

        mn_cad_titulo.setText("Titulo");
        mn_cad_titulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_tituloActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_titulo);

        mn_cad_genero.setText("Genero");
        mn_cad_genero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_generoActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_genero);

        mn_cad_produtora.setText("Produtora");
        mn_cad_produtora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_produtoraActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_produtora);

        mn_cad_midia.setText("Midia");
        mn_cad_midia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_midiaActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_midia);

        mn_cad_usuario.setText("Cadastro de Usuario");
        mn_cad_usuario.setEnabled(false);
        mn_cad_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cad_usuarioActionPerformed(evt);
            }
        });
        jMenu2.add(mn_cad_usuario);

        jMenuBar1.add(jMenu2);

        mn_retirada.setText("Retiradas");
        mn_retirada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_retiradaActionPerformed(evt);
            }
        });

        mn_todas_retirada.setText("Ver todas");
        mn_todas_retirada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_todas_retiradaActionPerformed(evt);
            }
        });
        mn_retirada.add(mn_todas_retirada);

        mn_cadastrar_Retirada.setText("Cadastrar");
        mn_cadastrar_Retirada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mn_cadastrar_RetiradaActionPerformed(evt);
            }
        });
        mn_retirada.add(mn_cadastrar_Retirada);

        jMenuBar1.add(mn_retirada);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 912, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mn_cad_midiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_midiaActionPerformed
        adicionarInternalFrame(new CadastroMidia());
    }//GEN-LAST:event_mn_cad_midiaActionPerformed

    private void mn_cad_clienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_clienteActionPerformed
        adicionarInternalFrame(new CadastroCliente());
    }//GEN-LAST:event_mn_cad_clienteActionPerformed

    private void mn_cad_tituloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_tituloActionPerformed
        adicionarInternalFrame(new CadastroFilme());
    }//GEN-LAST:event_mn_cad_tituloActionPerformed

    private void mn_cad_generoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_generoActionPerformed
        adicionarInternalFrame(new CadastroGenero());
    }//GEN-LAST:event_mn_cad_generoActionPerformed

    private void mn_cad_produtoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_produtoraActionPerformed
        adicionarInternalFrame(new CadastroProdutora());
    }//GEN-LAST:event_mn_cad_produtoraActionPerformed

    private void mn_retiradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_retiradaActionPerformed
    }//GEN-LAST:event_mn_retiradaActionPerformed

    private void mn_todas_retiradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_todas_retiradaActionPerformed
        adicionarInternalFrame(new Retiradas());
    }//GEN-LAST:event_mn_todas_retiradaActionPerformed

    private void mn_cadastrar_RetiradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cadastrar_RetiradaActionPerformed
        TelaPrincipal.adicionarInternalFrame(new FazerRetirada());
    }//GEN-LAST:event_mn_cadastrar_RetiradaActionPerformed

    private void mn_cad_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mn_cad_usuarioActionPerformed
        adicionarInternalFrame(new CadastroUsuario());
    }//GEN-LAST:event_mn_cad_usuarioActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
        new Login().setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        sair();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        sair();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private static javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem mn_cad_cliente;
    private javax.swing.JMenuItem mn_cad_genero;
    private javax.swing.JMenuItem mn_cad_midia;
    private javax.swing.JMenuItem mn_cad_produtora;
    private javax.swing.JMenuItem mn_cad_titulo;
    private javax.swing.JMenuItem mn_cad_usuario;
    private javax.swing.JMenuItem mn_cadastrar_Retirada;
    private javax.swing.JMenu mn_retirada;
    private javax.swing.JMenuItem mn_todas_retirada;
    // End of variables declaration//GEN-END:variables

    public static void adicionarInternalFrame(JInternalFrame cadastrocliente) {
        cadastrocliente.setVisible(true);
        jDesktopPane1.add(cadastrocliente);
        jDesktopPane1.getDesktopManager().activateFrame(cadastrocliente);
        cadastrocliente.requestFocus();
    }

    private void sair() throws HeadlessException {
        if (JOptionPane.showConfirmDialog(rootPane, "Deseja realmente sair do programa?", "Aviso!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }
}
